package com.zxcl.pay;

import com.alibaba.fastjson2.JSON;
import com.alipay.api.domain.AlipayAccount;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.demo.trade.config.Configs;
import com.alipay.demo.trade.model.builder.AlipayTradePrecreateRequestBuilder;
import com.alipay.demo.trade.model.result.AlipayF2FPrecreateResult;
import com.alipay.demo.trade.service.AlipayTradeService;
import com.alipay.demo.trade.service.impl.AlipayTradeServiceImpl;
import com.zxcl.model.AliPayModel;
import lombok.extern.log4j.Log4j2;

import java.util.HashMap;
import java.util.Map;


@Log4j2
public class PayService {

    // 支付宝当面付2.0服务
    private static AlipayTradeService tradeService;

    static {
        /** 一定要在创建AlipayTradeService之前调用Configs.init()设置默认参数
         *  Configs会读取classpath下的zfbinfo.properties文件配置信息，如果找不到该文件则确认该文件是否在classpath目录
         */
        Configs.init("zfbinfo.properties");
        /** 使用Configs提供的默认参数
         *  AlipayTradeService可以使用单例或者为静态成员对象，不需要反复new
         */
        tradeService = new AlipayTradeServiceImpl.ClientBuilder().build();
    }

    /**
     * 支付
     */
    public static Map<String,Object> pay(AliPayModel aliPayModel){
        Map<String,Object> resMap=new HashMap<>();
        resMap.put("code","200");
        resMap.put("msg","success");
        // 创建扫码支付请求builder，设置请求参数
        AlipayTradePrecreateRequestBuilder builder = new AlipayTradePrecreateRequestBuilder()
                .setSubject(aliPayModel.getSubject())
                .setTotalAmount(aliPayModel.getTotalAmount())
                .setOutTradeNo(aliPayModel.getOutTradeNo())
                .setUndiscountableAmount(aliPayModel.getUndiscountableAmount())
                .setSellerId(aliPayModel.getSellerId())
                .setBody(aliPayModel.getBody())
                .setOperatorId(aliPayModel.getOperatorId())
                .setStoreId(aliPayModel.getStoreId())
                .setTimeoutExpress(aliPayModel.getTimeoutExpress())
                .setNotifyUrl(aliPayModel.getNotifyUrl());//支付宝服务器主动通知商户服务器里指定的页面http路径,根据需要设置

        AlipayF2FPrecreateResult result = tradeService.tradePrecreate(builder);
        AlipayTradePrecreateResponse response = result.getResponse();
        switch (result.getTradeStatus()) {
            case SUCCESS:
                log.info("支付宝预下单成功: )");
                resMap.put("qrCode",response.getQrCode());
                resMap.put("outTradeNo",response.getOutTradeNo());
                break;
            case FAILED:
                log.error("支付宝预下单失败!!!");
                resMap.put("code",500);
                resMap.put("msg",response.getMsg());
                break;
            case UNKNOWN:
                log.error("系统异常，预下单状态未知!!!");
                resMap.put("code",500);
                resMap.put("msg",response.getMsg());
                break;
            default:
                log.error("不支持的交易状态，交易返回异常!!!");
                resMap.put("code",500);
                resMap.put("msg",response.getMsg());
                break;
        }
        return resMap;
    }

    /**
     * 退费
     */


}
